#define  _HT1622_GLOBAL_
#include "config.h"


#define CS_1622_DEINIT	GPIOA_MODE7=MODE_G_IN
#define SK_1622_DEINIT	GPIOA_MODE5=MODE_G_IN
#define DI_1622_DEINIT	GPIOA_MODE6=MODE_G_IN
#define CS_1622_INIT	GPIOA_MODE7=MODE_G_OUT;GPIOA_OSPEEDR7=SPEED_LEVEL_3
#define SK_1622_INIT	GPIOA_MODE5=MODE_G_OUT;GPIOA_OSPEEDR5=SPEED_LEVEL_3
#define DI_1622_INIT	GPIOA_MODE6=MODE_G_OUT;GPIOA_OSPEEDR6=SPEED_LEVEL_3

#define CS_1622_H    	GPIOA_MODE7=MODE_G_OUT;GPIOA_SET=(1<<7)
#define CS_1622_L			GPIOA_MODE5=MODE_G_OUT;GPIOA_CLR=(1<<7)
#define SK_1622_H    	GPIOA_MODE5=MODE_G_OUT;GPIOA_SET=(1<<5)
#define SK_1622_L			GPIOA_MODE5=MODE_G_OUT;GPIOA_CLR=(1<<5)
#define DI_1622_H    	GPIOA_MODE6=MODE_G_OUT;GPIOA_SET=(1<<6)
#define DI_1622_L			GPIOA_MODE6=MODE_G_OUT;GPIOA_CLR=(1<<6)

/**********************************************************************/
/*Name: Reset_1621() */
/*Function: */
/**********************************************************************/
void Reset_1622(void)
{
 CS_1622_H;
 SK_1622_H;
 DI_1622_H;
 DelayTimer(1);
 CS_1622_L; 
 CS_1622_L; 
 SK_1622_L; 
 SK_1622_L; 
}

/***********************************************************************/
/*Name: SendBit_HT1622() */
/*Function:  */
/**********************************************************************/
void SendBit_HT1622(unsigned char op_data,unsigned char num)
{
 unsigned char temp1;
 for(temp1=0;temp1<num;temp1++)
 {
 	if((op_data&0X80)==00)
 	{
		DI_1622_L;
	}
 	else
	{
		DI_1622_H;
	}
 	SK_1622_H; 
	SK_1622_H; 
	SK_1622_H; 
	SK_1622_H;
 	SK_1622_L;
 	op_data=op_data<<1;
 }
}

/*********************************************************************
A
*********************************************************************/
void SENDCOMA(unsigned char command)
{
 Reset_1622();
 SendBit_HT1622(0x080,4); 		/*ID=100 0*/
 SendBit_HT1622(command,8); 	/* send command*/
}

/*********************************************************************
B
*********************************************************************/
void SENDCOMB(unsigned char address)
{
 Reset_1622();
 DelayTimer(1);					/*delay 10us*/
 SendBit_HT1622(0xA0,4); 	/*ID=101 0*/
 SendBit_HT1622(address,5); /* send address*/
}

/*********************************************************************
C
*********************************************************************/
void SENDCOMC(unsigned char command)
{
 Reset_1622();
 SendBit_HT1622(0x90,4); 		/*ID=100 1*/
 SendBit_HT1622(command,8); 	/* send command*/
}

/*---------------------------------------------------------------------------*/
/*		Name:	EXLCDWRITE                                                  	 */
/* 		Func:	write *write_data size of data_length to LCD              	 */
/*		Date:	2005/04/27                                                   */
/*---------------------------------------------------------------------------*/
unsigned char LCDWRITE_1622(unsigned char *write_data, unsigned char data_length)
{
 unsigned char i;

 /* set address point 0*/
 SENDCOMB(0x00);
 /*writes data continuously*/
 for(i=0;i<data_length;i++)
	{
		SendBit_HT1622(*write_data++, 8);	
	}
	

 CS_1622_H;
 return(1);
}

/*-------------------------------------------------------------------------------+
 name        : initHT1622()
  function    : initial LCD controller / driver (HT1622)
+-------------------------------------------------------------------------------*/
void HT1622Init(void)
{
 CS_1622_INIT;
 SK_1622_INIT;
 DI_1622_INIT;
	
 SENDCOMA(LCD_1622_BIAS); 	/*set*/
 SENDCOMA(LCD_1622_RC256);	/*set*/
 SENDCOMA(LCD_1622_SYSEN); 	/*oscillator ON*/

 SENDCOMA(LCD_1622_LCDON); 	/*Display ON*/
 SENDCOMA(LCD_1622_TIMERDIS); /*Disable Timer Output*/

}

/*-------------------------------------------------------------------------------+
+-------------------------------------------------------------------------------*/
void DISP_ALL(unsigned char data)
{
 register char i;
 unsigned char str[16], *p;
 
 for(i = 0, p = str; i < 16; i++)
 	*p++ = data;

 LCDWRITE_1622(str, 16);
}

